package Tools.Drawing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.geom.Vector2f;

/** Font tag system class
 * @author Aikidos
 */
public class FontTagSystem {
	/** List of tag */
	static private Map<String, UnicodeFont> _lstTags = new HashMap<String, UnicodeFont>();
	
	public enum EAlign {
		LEFT,
		CENTER,
		RIGHT
	}
	
	/** Initialize */
	static public void init() throws SlickException {
		// Add standart tag
		UnicodeFont standartTag = new UnicodeFont("res/fonts/mainfont.ttf",
				11, false, false);
		standartTag.addAsciiGlyphs();
		standartTag.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
		standartTag.loadGlyphs();

		_lstTags.put("standart", standartTag);
	}
	
	/** Add new tag to list of tag
	 * @param tagName - Tag name
	 * @param font - Tag font
	 */
	static public void addTag(String tagName, UnicodeFont font) {
		_lstTags.put(tagName, font);
	}
	
	/** Draw text
	 * @param text - Text
	 * @param pos - Position
	 * @param align - Align
	 */
	static public void drawText(String text, Vector2f pos, EAlign align) {
		// Message words
		List<String> lstWords = new ArrayList<String>();

		// Open tags
		List<String> openTags = new ArrayList<String>();

		String symbols = "";
		for (int i = 0; i < text.length(); i++) {
			if (text.charAt(i) == '[') {
				if (text.charAt(i + 1) != '/') {
					// Add word
					if (symbols != "") {
						if (openTags.size() == 0)
							lstWords.add("[standart]" + symbols);
						else
							lstWords.add("["
									+ openTags.get(openTags.size() - 1) + "]"
									+ symbols);

						symbols = "";
					}

					// Get tag name
					String tagName = "";
					for (int j = i + 1; j < text.length(); j++) {
						if (text.charAt(j) != ']')
							tagName += text.charAt(j);
						else {
							openTags.add(tagName);

							i = j;
							break;
						}
					}

					continue;
				} else {
					// Add word
					if (symbols != "") {
						if (openTags.size() == 0)
							lstWords.add("[standart]" + symbols);
						else
							lstWords.add("["
									+ openTags.get(openTags.size() - 1) + "]"
									+ symbols);

						symbols = "";
					}

					// Get tag name
					String tagName = "";
					for (int j = i + 2; j < text.length(); j++) {
						if (text.charAt(j) != ']')
							tagName += text.charAt(j);
						else {
							// Close open tag
							for (int k = 0; k < openTags.size(); k++) {
								if (openTags.get(k).equals(tagName)) {
									openTags.remove(k);
									break;
								}
							}

							i = j;
							break;
						}
					}

					continue;
				}
			}

			symbols += text.charAt(i);
		}

		if (symbols != "")
			lstWords.add("[standart]" + symbols);

		// Get message width
		int messageWidth = 0;
		for (String word : lstWords) {
			// Get tag name
			String tagName = word.substring(word.indexOf("[") + 1,
					word.indexOf("]"));

			// Get text
			String t = word.substring(word.indexOf("]") + 1);

			// Get font
			UnicodeFont font = _lstTags.get(tagName);
			messageWidth += font.getWidth(t);
		}

		// Set align
		Vector2f drawPos = new Vector2f();
		drawPos.set(pos);
		
		switch (align) {
		case CENTER:
			drawPos.x = pos.x - (messageWidth / 2);
			break;
		case LEFT:
			break;
		case RIGHT:
			drawPos.x = pos.x - messageWidth;
			break;
		}
		
		// Draw words
		for (String word : lstWords) {
			// Get tag name
			String tagName = word.substring(word.indexOf("[") + 1,
					word.indexOf("]"));

			// Get text
			String t = word.substring(word.indexOf("]") + 1);

			// Draw text
			UnicodeFont drawFont = _lstTags.get(tagName);
			drawFont.drawString(drawPos.x, drawPos.y, t);

			drawPos.x += drawFont.getWidth(t);
		}
	}
	
	/** Draw text
	 * @param text - Text
	 * @param pos - Position
	 */
	static public void drawText(String text, Vector2f pos) {
		drawText(text, pos, EAlign.LEFT);
	}

}
